package com.jsm.twentyfour.controller;

import com.jsm.twentyfour.domain.Account;
import com.jsm.twentyfour.dto.ScoreDTO;
import com.jsm.twentyfour.reponse.WechatSnsResponse;
import com.jsm.twentyfour.request.WechatSnsRequest;
import com.jsm.twentyfour.service.AccountService;
import com.jsm.twentyfour.service.ScoreService;
import com.jsm.twentyfour.service.WechatService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * create by jsm on 2019/1/23
 */
@RestController
public class AccountController extends BaseController {
    @Resource
    private WechatService wechatService;
    @Resource
    private AccountService accountService;
    @Resource
    private ScoreService scoreService;

    @PostMapping("/account/login")
    public WechatSnsResponse login(@RequestBody WechatSnsRequest request) {
        WechatSnsResponse wechatSnsResponse = wechatService.oauth2GetOpenid(request.getCode());
        Account account = request.getAccount();
        account.setOpenId(wechatSnsResponse.getOpenid());
        accountService.login(account);
        ScoreDTO score = scoreService.getByUserId(account.getId());
        wechatSnsResponse.setUserId(account.getId());
        wechatSnsResponse.setScore(score.getScore());
        return wechatSnsResponse;
    }
}
